import { create } from "zustand"

import { ChatSession } from "@prisma/client"
import { getChatSessionById } from "@/data/chat"

type chatSessionState = {
    session: ChatSession | null

    sessionId: string
    setSessionId: (sessionId: string) => void
}

export const useChatSession = create<chatSessionState>((set, get) => ({
    session: null,

    sessionId: '',
    setSessionId: async (sessionId: string) => {
        if (get().sessionId !== sessionId) {
            const session = await getChatSessionById(sessionId)
            set({ sessionId, session })
        }
    }
}))